home *** CD-ROM | disk | FTP | other *** search
/ One Click 11 / OneClick11.iso / Bancos de Dados / Conversao / Mysql2Excel / Setup.exe / Mysql2Excel.exe / copy.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-06-23  |  12.3 KB  |  448 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.2)
  3.  
  4. '''Generic (shallow and deep) copying operations.
  5.  
  6. Interface summary:
  7.  
  8.         import copy
  9.  
  10.         x = copy.copy(y)        # make a shallow copy of y
  11.         x = copy.deepcopy(y)    # make a deep copy of y
  12.  
  13. For module specific errors, copy.error is raised.
  14.  
  15. The difference between shallow and deep copying is only relevant for
  16. compound objects (objects that contain other objects, like lists or
  17. class instances).
  18.  
  19. - A shallow copy constructs a new compound object and then (to the
  20.   extent possible) inserts *the same objects* into in that the
  21.   original contains.
  22.  
  23. - A deep copy constructs a new compound object and then, recursively,
  24.   inserts *copies* into it of the objects found in the original.
  25.  
  26. Two problems often exist with deep copy operations that don\'t exist
  27. with shallow copy operations:
  28.  
  29.  a) recursive objects (compound objects that, directly or indirectly,
  30.     contain a reference to themselves) may cause a recursive loop
  31.  
  32.  b) because deep copy copies *everything* it may copy too much, e.g.
  33.     administrative data structures that should be shared even between
  34.     copies
  35.  
  36. Python\'s deep copy operation avoids these problems by:
  37.  
  38.  a) keeping a table of objects already copied during the current
  39.     copying pass
  40.  
  41.  b) letting user-defined classes override the copying operation or the
  42.     set of components copied
  43.  
  44. This version does not copy types like module, class, function, method,
  45. nor stack trace, stack frame, nor file, socket, window, nor array, nor
  46. any similar types.
  47.  
  48. Classes can use the same interfaces to control copying that they use
  49. to control pickling: they can define methods called __getinitargs__(),
  50. __getstate__() and __setstate__().  See the documentation for module
  51. "pickle" for information on these methods.
  52. '''
  53. import types
  54.  
  55. class Error(Exception):
  56.     pass
  57.  
  58. error = Error
  59.  
  60. try:
  61.     from org.python.core import PyStringMap
  62. except ImportError:
  63.     PyStringMap = None
  64.  
  65. __all__ = [
  66.     'Error',
  67.     'error',
  68.     'copy',
  69.     'deepcopy']
  70.  
  71. def copy(x):
  72.     """Shallow copy operation on arbitrary Python objects.
  73.  
  74.     See the module's __doc__ string for more info.
  75.     """
  76.     
  77.     try:
  78.         copierfunction = _copy_dispatch[type(x)]
  79.     except KeyError:
  80.         
  81.         try:
  82.             copier = x.__copy__
  83.         except AttributeError:
  84.             
  85.             try:
  86.                 reductor = x.__reduce__
  87.             except AttributeError:
  88.                 raise error, 'un(shallow)copyable object of type %s' % type(x)
  89.  
  90.             y = _reconstruct(x, reductor(), 0)
  91.  
  92.         y = copier()
  93.  
  94.     y = copierfunction(x)
  95.     return y
  96.  
  97. _copy_dispatch = d = { }
  98.  
  99. def _copy_atomic(x):
  100.     return x
  101.  
  102. d[types.NoneType] = _copy_atomic
  103. d[types.IntType] = _copy_atomic
  104. d[types.LongType] = _copy_atomic
  105. d[types.FloatType] = _copy_atomic
  106.  
  107. try:
  108.     d[types.ComplexType] = _copy_atomic
  109. except AttributeError:
  110.     pass
  111.  
  112. d[types.StringType] = _copy_atomic
  113.  
  114. try:
  115.     d[types.UnicodeType] = _copy_atomic
  116. except AttributeError:
  117.     pass
  118.  
  119.  
  120. try:
  121.     d[types.CodeType] = _copy_atomic
  122. except AttributeError:
  123.     pass
  124.  
  125. d[types.TypeType] = _copy_atomic
  126. d[types.XRangeType] = _copy_atomic
  127. d[types.ClassType] = _copy_atomic
  128.  
  129. def _copy_list(x):
  130.     return x[:]
  131.  
  132. d[types.ListType] = _copy_list
  133.  
  134. def _copy_tuple(x):
  135.     return x[:]
  136.  
  137. d[types.TupleType] = _copy_tuple
  138.  
  139. def _copy_dict(x):
  140.     return x.copy()
  141.  
  142. d[types.DictionaryType] = _copy_dict
  143. if PyStringMap is not None:
  144.     d[PyStringMap] = _copy_dict
  145.  
  146.  
  147. def _copy_inst(x):
  148.     if hasattr(x, '__copy__'):
  149.         return x.__copy__()
  150.     
  151.     if hasattr(x, '__getinitargs__'):
  152.         args = x.__getinitargs__()
  153.         y = apply(x.__class__, args)
  154.     else:
  155.         y = _EmptyClass()
  156.         y.__class__ = x.__class__
  157.     if hasattr(x, '__getstate__'):
  158.         state = x.__getstate__()
  159.     else:
  160.         state = x.__dict__
  161.     if hasattr(y, '__setstate__'):
  162.         y.__setstate__(state)
  163.     else:
  164.         y.__dict__.update(state)
  165.     return y
  166.  
  167. d[types.InstanceType] = _copy_inst
  168. del d
  169.  
  170. def deepcopy(x, memo = None):
  171.     """Deep copy operation on arbitrary Python objects.
  172.  
  173.     See the module's __doc__ string for more info.
  174.     """
  175.     if memo is None:
  176.         memo = { }
  177.     
  178.     d = id(x)
  179.     if memo.has_key(d):
  180.         return memo[d]
  181.     
  182.     
  183.     try:
  184.         copierfunction = _deepcopy_dispatch[type(x)]
  185.     except KeyError:
  186.         
  187.         try:
  188.             copier = x.__deepcopy__
  189.         except AttributeError:
  190.             
  191.             try:
  192.                 reductor = x.__reduce__
  193.             except AttributeError:
  194.                 raise error, 'un-deep-copyable object of type %s' % type(x)
  195.  
  196.             y = _reconstruct(x, reductor(), 1, memo)
  197.  
  198.         y = copier(memo)
  199.  
  200.     y = copierfunction(x, memo)
  201.     memo[d] = y
  202.     return y
  203.  
  204. _deepcopy_dispatch = d = { }
  205.  
  206. def _deepcopy_atomic(x, memo):
  207.     return x
  208.  
  209. d[types.NoneType] = _deepcopy_atomic
  210. d[types.IntType] = _deepcopy_atomic
  211. d[types.LongType] = _deepcopy_atomic
  212. d[types.FloatType] = _deepcopy_atomic
  213.  
  214. try:
  215.     d[types.ComplexType] = _deepcopy_atomic
  216. except AttributeError:
  217.     pass
  218.  
  219. d[types.StringType] = _deepcopy_atomic
  220.  
  221. try:
  222.     d[types.UnicodeType] = _deepcopy_atomic
  223. except AttributeError:
  224.     pass
  225.  
  226.  
  227. try:
  228.     d[types.CodeType] = _deepcopy_atomic
  229. except AttributeError:
  230.     pass
  231.  
  232. d[types.TypeType] = _deepcopy_atomic
  233. d[types.XRangeType] = _deepcopy_atomic
  234.  
  235. def _deepcopy_list(x, memo):
  236.     y = []
  237.     memo[id(x)] = y
  238.     for a in x:
  239.         y.append(deepcopy(a, memo))
  240.     
  241.     return y
  242.  
  243. d[types.ListType] = _deepcopy_list
  244.  
  245. def _deepcopy_tuple(x, memo):
  246.     y = []
  247.     for a in x:
  248.         y.append(deepcopy(a, memo))
  249.     
  250.     d = id(x)
  251.     
  252.     try:
  253.         return memo[d]
  254.     except KeyError:
  255.         pass
  256.  
  257.     for i in range(len(x)):
  258.         if x[i] is not y[i]:
  259.             y = tuple(y)
  260.             break
  261.         
  262.     else:
  263.         y = x
  264.     memo[d] = y
  265.     return y
  266.  
  267. d[types.TupleType] = _deepcopy_tuple
  268.  
  269. def _deepcopy_dict(x, memo):
  270.     y = { }
  271.     memo[id(x)] = y
  272.     for key in x.keys():
  273.         y[deepcopy(key, memo)] = deepcopy(x[key], memo)
  274.     
  275.     return y
  276.  
  277. d[types.DictionaryType] = _deepcopy_dict
  278. if PyStringMap is not None:
  279.     d[PyStringMap] = _deepcopy_dict
  280.  
  281.  
  282. def _keep_alive(x, memo):
  283.     '''Keeps a reference to the object x in the memo.
  284.  
  285.     Because we remember objects by their id, we have
  286.     to assure that possibly temporary objects are kept
  287.     alive by referencing them.
  288.     We store a reference at the id of the memo, which should
  289.     normally not be used unless someone tries to deepcopy
  290.     the memo itself...
  291.     '''
  292.     
  293.     try:
  294.         memo[id(memo)].append(x)
  295.     except KeyError:
  296.         memo[id(memo)] = [
  297.             x]
  298.  
  299.  
  300.  
  301. def _deepcopy_inst(x, memo):
  302.     if hasattr(x, '__deepcopy__'):
  303.         return x.__deepcopy__(memo)
  304.     
  305.     if hasattr(x, '__getinitargs__'):
  306.         args = x.__getinitargs__()
  307.         _keep_alive(args, memo)
  308.         args = deepcopy(args, memo)
  309.         y = apply(x.__class__, args)
  310.     else:
  311.         y = _EmptyClass()
  312.         y.__class__ = x.__class__
  313.     memo[id(x)] = y
  314.     if hasattr(x, '__getstate__'):
  315.         state = x.__getstate__()
  316.         _keep_alive(state, memo)
  317.     else:
  318.         state = x.__dict__
  319.     state = deepcopy(state, memo)
  320.     if hasattr(y, '__setstate__'):
  321.         y.__setstate__(state)
  322.     else:
  323.         y.__dict__.update(state)
  324.     return y
  325.  
  326. d[types.InstanceType] = _deepcopy_inst
  327.  
  328. def _reconstruct(x, info, deep, memo = None):
  329.     if isinstance(info, str):
  330.         return x
  331.     
  332.     if not __debug__ and isinstance(info, tuple):
  333.         raise AssertionError
  334.     if memo is None:
  335.         memo = { }
  336.     
  337.     n = len(info)
  338.     if not __debug__ and n in (2, 3):
  339.         raise AssertionError
  340.     (callable, args) = info[:2]
  341.     if n > 2:
  342.         state = info[2]
  343.     else:
  344.         state = { }
  345.     if deep:
  346.         args = deepcopy(args, memo)
  347.     
  348.     y = callable(*args)
  349.     if state:
  350.         if deep:
  351.             state = deepcopy(state, memo)
  352.         
  353.         y.__dict__.update(state)
  354.     
  355.     return y
  356.  
  357. del d
  358. del types
  359.  
  360. class _EmptyClass:
  361.     pass
  362.  
  363.  
  364. def _test():
  365.     l = [
  366.         None,
  367.         1,
  368.         0x2L,
  369.         3.1400000000000001,
  370.         'xyzzy',
  371.         (1, 0x2L),
  372.         [
  373.             3.1400000000000001,
  374.             'abc'],
  375.         {
  376.             'abc': 'ABC' },
  377.         (),
  378.         [],
  379.         { }]
  380.     l1 = copy(l)
  381.     print l1 == l
  382.     l1 = map(copy, l)
  383.     print l1 == l
  384.     l1 = deepcopy(l)
  385.     print l1 == l
  386.     
  387.     class C:
  388.         
  389.         def __init__(self, arg = None):
  390.             self.a = 1
  391.             self.arg = arg
  392.             if __name__ == '__main__':
  393.                 import sys
  394.                 file = sys.argv[0]
  395.             else:
  396.                 file = __file__
  397.             self.fp = open(file)
  398.             self.fp.close()
  399.  
  400.         
  401.         def __getstate__(self):
  402.             return {
  403.                 'a': self.a,
  404.                 'arg': self.arg }
  405.  
  406.         
  407.         def __setstate__(self, state):
  408.             for key in state.keys():
  409.                 setattr(self, key, state[key])
  410.             
  411.  
  412.         
  413.         def __deepcopy__(self, memo = None):
  414.             new = self.__class__(deepcopy(self.arg, memo))
  415.             new.a = self.a
  416.             return new
  417.  
  418.  
  419.     c = C('argument sketch')
  420.     l.append(c)
  421.     l2 = copy(l)
  422.     print l == l2
  423.     print l
  424.     print l2
  425.     l2 = deepcopy(l)
  426.     print l == l2
  427.     print l
  428.     print l2
  429.     l.append({
  430.         l[1]: l,
  431.         'xyz': l[2] })
  432.     l3 = copy(l)
  433.     import repr
  434.     print map(repr.repr, l)
  435.     print map(repr.repr, l1)
  436.     print map(repr.repr, l2)
  437.     print map(repr.repr, l3)
  438.     l3 = deepcopy(l)
  439.     import repr
  440.     print map(repr.repr, l)
  441.     print map(repr.repr, l1)
  442.     print map(repr.repr, l2)
  443.     print map(repr.repr, l3)
  444.  
  445. if __name__ == '__main__':
  446.     _test()
  447.  
  448.